package com.zuihai.app.mvp.ui.activity.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zuihai.app.R;
import com.zuihai.app.mvp.constant.DetailConstant;
import com.zuihai.app.mvp.model.entity.FindItem;
import com.zuihai.app.mvp.model.entity.mainhome.SubclassDataSupport;
import com.zuihai.app.mvp.ui.activity.detail.DetailContainerActivity;
import com.zuihai.app.mvp.ui.activity.find.FindRecyclerFragment;
import com.zuihai.app.mvp.ui.adapter.FindHomeRecyclerAdapter;
import com.zuihai.app.mvp.ui.utils.DividerListItemDecoration;
import com.zuihai.app.mvp.ui.widget.MyToastPK;
import com.zuihai.app.mvp.ui.widget.SimpleLoadMore;


import org.litepal.crud.DataSupport;

import java.util.Calendar;
import java.util.List;

/**
 * Created by Zc on 2017/5/3.
 * 首页-推荐
 */

public class HomeRecommendFragment extends FindRecyclerFragment {
    public static HomeRecommendFragment newInstance() {
        Bundle args = new Bundle();
        HomeRecommendFragment fragment = new HomeRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void requestData(boolean isRefresh) {
        mPresenter.requestHomeRecommend(isRefresh);
    }

    @Override
    public void bindFHAdapter(FindHomeRecyclerAdapter adapter) {
        adapter.setOnItemClickListener(this);
        //设置加载更多
        adapter.setOnLoadMoreListener(getmLoadMoreListener(), getRecycleView());
        adapter.openLoadAnimation();
        adapter.setLoadMoreView(new SimpleLoadMore());
        getRecycleView().setAdapter(adapter);
        //添加布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        getRecycleView().setLayoutManager(layoutManager);
        //添加分割线
        DividerListItemDecoration decoration = new DividerListItemDecoration(getContext(), LinearLayoutManager.VERTICAL, R.drawable.common_divider_padding14_shape);
        getRecycleView().addItemDecoration(decoration);
        getRecycleView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (getNeedExpendListener() != null && dy < 0 && ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0) {
                    getNeedExpendListener().needExpand();
                }
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemClick(adapter, view, position);
        FindItem data = (FindItem) adapter.getItem(position);
        String detailId = data.discoveryId;
        String type=data.type101+"";
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            LogUtils.e("点击："+detailId+"   "+type);
            List<SubclassDataSupport> allHomeColus = DataSupport.findAll(SubclassDataSupport.class);
            for (int i = 0; i < allHomeColus.size(); i++) {
                if (allHomeColus.get(i).getSubitem_id().equals(type)){
                    if (allHomeColus.get(i).getLeibie() == 1) {
                        DetailContainerActivity.
                                launchActivity(getActivity(),
                                        DetailConstant.HEADLINE_TYPE,type, detailId);

                    } else if (allHomeColus.get(i).getLeibie() == 2) {
                        DetailContainerActivity.
                                launchActivity(getActivity(),
                                        DetailConstant.PICTURE_TYPE, detailId);
                    } else {
                        MyToastPK.showSuccess("该栏目暂不支持跳转", getActivity());
                    }
                }
            }
        }
    }
}
